package com.fucking.great.context.common.http;

import cn.hutool.http.ContentType;
import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpRequestCommon extends HttpUtil {

    public static final String JSON_TYPE = ContentType.JSON.toString();
    public static final String XML_TYPE = ContentType.XML.toString();

    protected class RequestData{
        public final String url;
        public final String body;
        public final String type;
        public final int timeout;

        public RequestData(String url,String body, String type, int timeout) {
            this.url = url;
            this.body = body;
            this.type = type;
            this.timeout = timeout;
        }
    }

    private List<RequestData> queue = new ArrayList<>();

    /**
     * 执行事务类型的http.  json请求类型
     * @param urlString
     * @param paramMap
     */
    public void executePostJson(String urlString,  Map<String, Object> paramMap) {
        executePostJson(urlString,paramMap,HttpGlobalConfig.getTimeout());
    }

    /**
     * 执行事务类型的http.  json请求类型
     * @param urlString
     * @param paramMap
     * @param timeout
     */
    public void executePostJson(String urlString,  Map<String, Object> paramMap, int timeout) {
        String body = this.toJsonStr(paramMap);
        RequestData requestData = new RequestData(urlString,body,JSON_TYPE,timeout);
        queue.add(requestData);
    }

    /**
     * 执行事务类型的http.  Xml请求类型
     * @param urlString
     * @param rootTag
     * @param paramMap
     */
    public void executePostXml(String urlString, String rootTag , Map<String, Object> paramMap) {
        executePostXml(urlString,rootTag,paramMap,HttpGlobalConfig.getTimeout());
    }

    /**
     * 执行事务类型的http.  Xml请求类型
     * @param urlString
     * @param rootTag
     * @param paramMap
     * @param timeout
     */
    public void executePostXml(String urlString, String rootTag ,  Map<String, Object> paramMap, int timeout) {
        String body = this.toXmlStr(rootTag, paramMap);
        RequestData requestData = new RequestData(urlString,body,XML_TYPE,timeout);
        queue.add(requestData);
    }
    public void realExecute(){
        for (RequestData requestData : queue) {
            /*if ( XML_TYPE.equals(requestData.type) ){

            }else if (JSON_TYPE.equals(requestData.type)){

            }*/
            post(requestData.url,requestData.body,requestData.timeout);
            // 采用异步的形式去调用。
            HttpRequest.post(requestData.url).body(requestData.body).timeout(requestData.timeout).execute(true);
        }
        queue.clear();
    }

    /**
     * 用json格式请求
     * @param urlString
     * @param paramMap
     * @param timeout
     * @return
     */
    public String postJson(String urlString,  Map<String, Object> paramMap, int timeout) {
        String body = toJsonStr(paramMap);
        return   HttpRequest.post(urlString).body(body).timeout(timeout).execute().body();
    }

    /**
     * map2json Str
     * @param paramMap
     * @return
     */
    public String toJsonStr(Map<String, Object> paramMap) {
        JSONObject jsonObject;
        if (paramMap instanceof JSONObject){
            jsonObject = (JSONObject) paramMap;
        }else{
            jsonObject = new JSONObject(paramMap);
        }
        String body = jsonObject.toString();
        return body;
    }

    /**
     * 用json格式请求
     * @param urlString
     * @param paramMap
     * @return
     */
    public String postJson(String urlString,  Map<String, Object> paramMap) {
        return  postJson(urlString,paramMap,HttpGlobalConfig.getTimeout());
    }
    /**
     * 用xml格式请求
     * @param urlString
     * @param rootTag
     * @param paramMap
     * @param timeout
     * @return
     */
    public String postXml(String urlString, String rootTag , Map<String, Object> paramMap, int timeout) {
        String body = toXmlStr(rootTag, paramMap);
        return   HttpRequest.post(urlString).body(body).timeout(timeout).execute().body();
    }
    /**
     * map2xml Str
     * @param paramMap
     * @return
     */
    public String toXmlStr(String rootTag, Map<String, Object> paramMap) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.set(rootTag, paramMap);
        String body = JSONUtil.toXmlStr(jsonObject);
        return body;
    }

    /**
     * 用xml格式请求
     * @param urlString
     * @param rootTag
     * @param paramMap
     * @return
     */
    public String postXml(String urlString, String rootTag , Map<String, Object> paramMap) {
        return  postXml(urlString,rootTag,paramMap,HttpGlobalConfig.getTimeout());
    }


}
